define(function(require){
  'use strict';
  var $ = require('jquery');
  var _const = require('seajs/fn/fnConst');
  var fnUtil = require('seajs/fn/fnUtils');
  require('seajs/plugs/jquery.md5')($);
  require('seajs/plugs/jquery.cookie')($);
  require('seajs/plugs/jquery.bootstrap-pureClearButton.min')($);

  var isMd5 = false;

  var init = function(){
    inputInit();

  	$('input[type="text"]').on('blur', function(){
  	  checkUser(this.value);
  	}).on('input', function(){//非IE浏览器，input值改变时触发
	  changeUsc(this);
  	}).on('propertychange', function(){//兼容IE
  	  changeUsc(this);
  	}).on('focus', function(){
  	  clearError();
	  });

  	$('input[type="password"]').on('blur', function(){
  	  checkPwd(this.value);
  	}).on('focus', function(){
	  clearError();
	});

  	$('.form-login').submit(function(e){
  	  e.preventDefault();
  	  var userCode = e.target[0].value;
  	  if(!checkUser(userCode)){
  	  	return false;
  	  }
  	  var pwd = e.target[1].value;
  	  if(!checkPwd(pwd)){
  	  	return false;
  	  }
      var _pwd;
      if(pwd.length === 32){
        _pwd = pwd;
      }else{
        _pwd = $.md5(pwd);
      }
  	  
  	  var _data = fnUtil.getToken({
  		  'userCode': userCode,
  		  'password': _pwd,
  		  'timestamp': fnUtil.getUnixTime(),
  		  'userType': _const.DEVICE.DISPATCHER,
        'deviceId': ''
  		}, _const.FN_KEY);
  		$.ajax({  
  		  type:'POST',
  		  url:'http://' 
  		    + _const.HTTP.CONFIG.HOST + ':' 
  		    + _const.HTTP.CONFIG.PORT + '/' 
  		    + _const.HTTP.CONFIG.PATH + 'app/login.do',
  		  data: _data,
  		  dataType:'json',
  		  timeout: 5000,
  		  success: function(rsp){
  		  	resultHandler(userCode, _pwd, rsp);
  		  },
  		  error: function(e){

  		  }
  		});
  	});
  }

  var clearPwd = function(){
	$('input[type="password"]').val('');
  };

  var fillPwd = function(uc){
	$('input[type="password"]').val(fnUtil.getQueryString(_const.LOCAL.AUTO_PWD));
  };

  var changeUsc = function(e){
    var uc = fnUtil.getLocal(_const.LOCAL.AUTO_USER);
    if(uc){
	  if(uc != $(e).val()){
		clearPwd();
	  }else{
		  inputInit();
	  }
    }else{
	  clearPwd();
    }
  }

  var showError = function(msg){
  	$('.error-info').html(msg).removeClass('vis-hid');
  };

  var clearError = function(){
  	$('.error-info').html('error').addClass('vis-hid');
  };

  var checkUser = function(value){
  	if(value === null || value === undefined || value === ''){
  	  showError('账号不能为空!');
  	  return false;
  	}
  	clearError();
    return true;
  }

  var checkPwd = function(pwd){
  	if(pwd.length !== 32 && (pwd.length < 6 || pwd.length > 16)){
  	  showError('密码长度在6-16位!');
  	  return false;
  	}
    clearError();
    return true;
  }

  var resultHandler = function(userCode, pwd, data){
	  console.log(data);
  	if(data.status === _const.HTTP.RES_STATUS.SUCCESS){
  	  var body = data.body;
  	  if($('#remember-me').is(':checked')){
  	  	fnUtil.saveLocal(_const.LOCAL.REMEMBER_ME, true, 7);
  	  	fnUtil.saveLocal(_const.LOCAL.AUTO_USER, userCode, 7);
  	  	fnUtil.saveLocal(userCode, {userCode: userCode, password:pwd}, 7);
  	  }else{
        fnUtil.delLocal(_const.LOCAL.REMEMBER_ME);
  	  	fnUtil.delLocal(_const.LOCAL.AUTO_USER);
  	  };
      console.log(body);
  	  location.href = 'index.html?userName=' + escape(body.userName) +
  	  	'&companyId=' + body.companyId +
  	  	'&companyName=' + escape(body.companyName) +
  	  	'&userid=' + body.userid +
  	  	'&userCode=' + userCode +
  	  	'&userToken=' + body.userToken +
        // '&defaultGid=' + body.defaultGroupId +
        '&defaultGid=' + body.defaultGroupId +
  	  	'&passport=' + pwd;
  	}else{
  	  showError(data.error.message);
  	}
  }

  var inputInit = function(){
    var isRememberMe = fnUtil.getLocal(_const.LOCAL.REMEMBER_ME);
    if(isRememberMe){
      $('#remember-me').attr('checked', true);
      var autoUser = fnUtil.getLocal(_const.LOCAL.AUTO_USER);
      if(autoUser){
        var userJson = fnUtil.getLocal(autoUser);
        $('input[type="text"]').val(userJson.userCode);
        $('input[type="password"]').val(userJson.password);
      }
    }
  }

  init();
});
